import os
os.environ["QT_API"] = "pyqt5"
import matplotlib
matplotlib.use("Qt5Agg") 

import sys
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as Fcq
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT  

class CustomizeToolBar(NavigationToolbar2QT):
    """
    用来重构matplotlib类的工具栏
    若要修改,请看NavigationToolbar2QT.toolitems
    """
    def __init__(self, canvas_, parent_):
        self.toolitems = (
            ('Home', 'Lorem ipsum dolor sit amet', 'home', 'home'),
            ('Back', 'consectetuer adipiscing elit', 'back', 'back'),
            ('Forward', 'sed diam nonummy nibh euismod', 'forward', 'forward'),
            ('Pan', 'tincidunt ut laoreet', 'move', 'pan'),
            ('Zoom', 'dolore magna aliquam', 'zoom_to_rect', 'zoom'),
            ('Save', 'sollemnes in futurum', 'filesave', 'save_figure'),
            )
        NavigationToolbar2QT.__init__(self, canvas_, parent_)

class TimeCanvas:
    """
    定义时域图
    """

    def __init__(self):
        # 窗口
        self.widget = QWidget()
        # 图控件
        self.fig = Figure(figsize=(8, 10), dpi=110, facecolor='white')
        self.canvas = Fcq(self.fig)
        self.axes = self.fig.add_subplot(111)
        # 工具栏控件
        self.toolbar = CustomizeToolBar(self.canvas, self.widget)
        # 布局
        self.layout = QVBoxLayout()
        # 运行函数
        self.init_TimeCanvas()

    def init_TimeCanvas(self):
        # 窗口大小
        self.widget.resize(600, 400)  # 设置窗口大小
        # 设置图控件
        self.fig.subplots_adjust(top=0.915, bottom=0.155, left=0.11, right=0.955, hspace=0.2, wspace=0.2)
        self.axes.set_title('Time Diagram', pad=0, loc='center', size=20, color='red')
        self.axes.set_xlabel('Time(s)', fontsize='x-large', color='red')  
        self.axes.set_ylabel('Amplitude(V)', fontsize='x-large', color='red')
        self.axes.grid(ls='--')  # 生成网格
        self.canvas.draw()
        # 工具栏控件
        self.toolbar.update()
        # 将图控件、工具栏控件加入布局
        self.layout.addWidget(self.toolbar)
        self.layout.addWidget(self.canvas)
        # 设置布局
        self.layout.setContentsMargins(0, 0, 0, 0)
        # 将布局加入窗口
        self.widget.setLayout(self.layout)
        # 显示窗口
        self.time_widget.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    my_gui = TimeCanvas()
    sys.exit(app.exec_())